﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmEditStudent : Form
    {
        public FrmEditStudent()
        {
            InitializeComponent();
        }

        private string sno = null;

        //public FrmEditStudent(string _Sno)
        //{
        //    InitializeComponent();
        //    sno = _Sno;
        //}

        private void FrmEditStudent_Load(object sender, EventArgs e)
        {
            InitStudentList();
            
        }

        private void InitStudentList()
        {
            if (this.Tag!=null&&this.Tag.ToString()!="")
            {
                sno = this.Tag.ToString().Trim();
            }
            string sql = "select Sno,Sname,Ssex,Sage,Sdept from Student where Sno=@Sno ";
            SqlParameter para = new SqlParameter("@Sno", sno);

            SqlDataReader dr = SqlHelper.ExecuteReader(sql, para);
            //读取数据 只向前，不能后退，读一条，丢一条
            if (dr.Read())
            {
                txtSno.Text = dr["Sno"].ToString().Trim();
                txtSname.Text = dr["Sname"].ToString().Trim();
                txtSdept.Text = dr["Sdept"].ToString().Trim();
                string ssex = dr["Ssex"].ToString();
                if (ssex == "男")
                {
                    rbBoy.Checked = true;
                }
                else if (ssex == "女")
                {
                    rbGirl.Checked = true;
                }
                else
                {
                    rbNull.Checked = true;
                }
                int sage = 0;
                if (dr["Sage"].ToString().Trim()!=""|| dr["Sage"]==null)
                {
                    sage = int.Parse(dr["Sage"].ToString().Trim());
                }

                string year = (DateTime.Today.Year - sage).ToString()+ "/1/1";
                dateTimePicker.Value = DateTime.Parse(year);
            }
            dr.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string snonew = txtSno.Text.Trim();
            string sname = txtSname.Text.Trim();
            string ssex = rbNull.Text;
            int sage = DateTime.Today.Year - dateTimePicker.Value.Year;
            string sdept = txtSdept.Text.Trim();

            if (string.IsNullOrEmpty(snonew))
            {
                MessageBox.Show("学号不能为空", "添加学号提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(sname))
            {
                MessageBox.Show("姓名不能为空", "添加姓名提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (rbBoy.Checked == true)
            {
                ssex = rbBoy.Text;
            }
            if (rbGirl.Checked == true)
            {
                ssex = rbGirl.Text;
            }
            if (rbNull.Checked == true)
            {
                ssex = rbNull.Text;
            }

            {
                string sqlSno = "select count(Sno) from Student where Sno=@Sno";
                SqlParameter[] paras =
                {
                    new SqlParameter("@Sno",sno)
                };
                object oSno = SqlHelper.ExecuteScalar(sqlSno, paras);
                if (oSno != null || oSno != DBNull.Value||(int)oSno!=0)
                {
                    string sqlAdd = "update Student set Sno=@SnoNew,Sname=@Sname,Ssex=@Ssex,Sage=@Sage,Sdept=@Sdept where Sno=@Sno  ";
                    SqlParameter[] parasAdd =
                    {
                        new SqlParameter("@Sno",sno),
                        new SqlParameter("@SnoNew",snonew),
                        new SqlParameter("@Sname",sname),
                        new SqlParameter("@Ssex",ssex),
                        new SqlParameter("@Sage",sage),
                        new SqlParameter("@Sdept",sdept)
                    };
                    int count = SqlHelper.ExecutNonQuery(sqlAdd, parasAdd);

                    if (sage == 0)
                    {
                        string sqlSage = "update Student set Sage=null where Sage=0 ";
                        int countSage = SqlHelper.ExecutNonQuery(sqlSage);
                        if (countSage == 0)
                        {
                            MessageBox.Show("学生信息修改失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (ssex == rbNull.Text)
                    {
                        string sqlSage = "update Student set Ssex=null where Ssex='空' ";
                        int countSsex = SqlHelper.ExecutNonQuery(sqlSage);
                        if (countSsex == 0)
                        {
                            MessageBox.Show("学生信息修改失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (count > 0)
                    {
                        MessageBox.Show("学生信息修改成功", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("学生信息修改失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }


                }
                else
                {
                    MessageBox.Show("学生信息无效", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
    }
}
